package com.fare.taluo.core.domain.analyse.report.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.fare.taluo.core.domain.analyse.report.CsairReportEntity;
import com.fare.taluo.core.domain.analyse.report.CsairReportEntityRepository;
import com.fare.taluo.fetl.domain.base.EntityFetlHibernateRepository;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.util.StringAndDateAndFileUtil;
@Repository
public class CsairReportEntityRepositoryImpl extends EntityFetlHibernateRepository implements CsairReportEntityRepository{

	@Override
	public List<CsairReportEntity> findCsairEntityAll(String dateStr) {
		try {
//			List<Object> list = new ArrayList<Object>();
			List<Object> params = new ArrayList<Object>();
			String sbf = "from com.fare.taluo.core.domain.analyse.report.CsairReportEntity fm where 1=1";
			if(StringAndDateAndFileUtil.checkString(dateStr)){
//				sbf+= " and fm.flightDate =str_to_date(?,'%Y-%m-%d')" ;
//				params.add(dateStr);
				sbf+= " and fm.flightDate = ?" ;
				params.add(DateUtils.parseDate(dateStr, "yyyy-MM-dd"));
			}
			
			sbf+=" and fm.deprotDate = ?";
			Date date = new Date();  
		    SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd");//24小时制 
		    String LgTime = sdformat.format(date);  
			params.add(DateUtils.parseDate(LgTime, "yyyy-MM-dd"));
			
			sbf+=" and fm.type =2 ";
			sbf+=" order by fm.flightDate, fm.flightType,fm.fromCity,fm.toCity,fm.price desc" ;
			Session session =getSessionFactory1().openSession();
			List<CsairReportEntity> list = null;
			try {
				Query query = session.createQuery(sbf);
				for (int i = 0; i < params.size(); i++) {
					query = query.setParameter(i, params.get(i));
				}
				 list = query.list();
			} catch (Exception e) {
				e.printStackTrace();	
			}finally{
				session.close();
			}
			return list;
			}catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		
//		List<CsairReportEntity> lists=super.find(sbf.toString(),list.toArray(),CsairReportEntity.class);
	}
	@Override
	public List<CsairReportEntity> findCsairEntityByCondition(String hbaseKey,String fromCityCode,String toCityCode,String startDate,String endDate,String flightDate,String grabDate,  String grabHbaseKey,String type) {
		List<Object> params = new ArrayList<Object>();
		StringBuffer sbf = new StringBuffer("from CsairReportEntity fm where 1=1");
		
		if(StringAndDateAndFileUtil.checkString(hbaseKey)){
			sbf.append(" and fm.hbaseKey = ?");
			params.add(hbaseKey);
		}
		if(StringAndDateAndFileUtil.checkString(fromCityCode)){
			sbf.append(" and fm.fromCity = ?");
			params.add(fromCityCode);
		}
		if(StringAndDateAndFileUtil.checkString(toCityCode)){
			sbf.append(" and fm.toCity = ?");
			params.add(toCityCode);
		}
		if(StringAndDateAndFileUtil.checkString(startDate)){
				sbf.append(" and fm.flightDate >= ? ");
				Date date = DateUtils.strToDate(startDate,"yyyy-MM-dd");
				params.add(date);
		}
		if(StringAndDateAndFileUtil.checkString(endDate)){
				sbf.append(" and fm.flightDate <= ? ");
				Date date = DateUtils.strToDate(endDate,"yyyy-MM-dd");
				params.add(date);
		}
		if(StringAndDateAndFileUtil.checkString(flightDate)){
				sbf.append(" and fm.flightDate = ? ");
				Date date = DateUtils.strToDate(flightDate,"yyyy-MM-dd");
				params.add(date);
		}
		if(StringAndDateAndFileUtil.checkString(grabDate)){
				sbf.append(" and fm.deprotDate = ? ");
				Date date = DateUtils.strToDate(grabDate,"yyyy-MM-dd");
				params.add(date);
		}
		if(StringAndDateAndFileUtil.checkString(grabHbaseKey)){
			sbf.append(" and fm.grabHbaseKey = ?");
			params.add(grabHbaseKey);
		}
		if(StringAndDateAndFileUtil.checkString(type)){
			sbf.append(" and fm.type = ?");
			params.add(Integer.parseInt(type));
		}
		sbf.append("  order by fm.fromCity,fm.toCity, fm.flightType, fm.flightNo,fm.flightDate,fm.deprotDate ");
		
		Session session =getSessionFactory1().openSession();
		List<CsairReportEntity> list = null;
		try {
			Query query = session.createQuery(sbf.toString());
			for (int i = 0; i < params.size(); i++) {
			query = query.setParameter(i, params.get(i));
			}
			 list = query.list();
		} catch (Exception e) {
			e.printStackTrace();	
		}finally{
			session.close();
		}
		
		return list;
	}
	

}
